home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / time.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  120 lines

  1. /* time - time a command    Authors: Andy Tanenbaum & Michiel Huisjes */
  2.  
  3. #include <sys/types.h>
  4. #include <sys/times.h>
  5. #include <limits.h>
  6. #include <time.h>
  7. #include <signal.h>
  8.  
  9. #define HZ CLOCKS_PER_SEC
  10.  
  11. char **args;
  12. char *name;
  13.  
  14. int digit_seen;
  15. char a[] = "        .  \n";
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.  
  22.   struct tms pre_buf, post_buf;
  23.   int status, pid;
  24.   long start_time, end_time;
  25.  
  26.   if (argc == 1) exit(0);
  27.  
  28.   args = &argv[1];
  29.   name = argv[1];
  30.  
  31.   /* Get real time at start of run. */
  32.   (void) time(&start_time);
  33.  
  34.   /* Fork off child. */
  35.   if ((pid = fork()) < 0) {
  36.     std_err("Cannot fork\n");
  37.     exit(1);
  38.   }
  39.   if (pid == 0) execute();
  40.  
  41.   /* Parent is the time program.  Disable interrupts and wait. */
  42.   signal(SIGINT, SIG_IGN);
  43.   signal(SIGQUIT, SIG_IGN);
  44.  
  45.   do {
  46.     times(&pre_buf);
  47.   } while (wait(&status) != pid);
  48.   (void) time(&end_time);
  49.  
  50.   if ((status & 0377) != 0) std_err("Command terminated abnormally.\n");
  51.   times(&post_buf);
  52.  
  53.   /* Print results. */
  54.   print_time("real ", (end_time - start_time) * HZ);
  55.   print_time("user ", post_buf.tms_cutime - pre_buf.tms_cutime);
  56.   print_time("sys  ", post_buf.tms_cstime - pre_buf.tms_cstime);
  57.   exit(status >> 8);
  58. }
  59.  
  60. print_time(mess, t)
  61. char *mess;
  62. register long t;
  63. {
  64. /* Print the time 't' in hours: minutes: seconds.  't' is in ticks. */
  65.   int hours, minutes, seconds, hundredths, i;
  66.  
  67.   digit_seen = 0;
  68.   for (i = 0; i < 8; i++) a[i] = ' ';
  69.   hours = (int) (t / (3600L * (long) HZ));
  70.   t -= (long) hours * 3600L * (long) HZ;
  71.   minutes = (int) (t / (60L * (long) HZ));
  72.   t -= (long) minutes * 60L * (long) HZ;
  73.   seconds = (int) (t / (long) HZ);
  74.   t -= (long) seconds * (long) HZ;
  75.   hundredths = (int) (t * 100L / (long) HZ);
  76.  
  77.   std_err(mess);
  78.  
  79.   if (hours) {
  80.     twin(hours, &a[0]);
  81.     a[2] = ':';
  82.   }
  83.   if (minutes || digit_seen) {
  84.     twin(minutes, &a[3]);
  85.     a[5] = ':';
  86.   }
  87.   if (seconds || digit_seen)
  88.     twin(seconds, &a[6]);
  89.   else
  90.     a[7] = '0';
  91.   a[9] = hundredths / 10 + '0';
  92.   a[10] = hundredths % 10 + '0';
  93. #ifndef HUNDREDTHS        /* tenths used to be enough */
  94.   a[10] = '\n'; a[11] = 0;
  95. #endif
  96.   std_err(a);
  97. }
  98.  
  99. twin(n, p)
  100. int n;
  101. char *p;
  102. {
  103.   char c1, c2;
  104.   c1 = (n / 10) + '0';
  105.   c2 = (n % 10) + '0';
  106.   if (digit_seen == 0 && c1 == '0') c1 = ' ';
  107.   *p++ = c1;
  108.   *p++ = c2;
  109.   if (n > 0) digit_seen = 1;
  110. }
  111.  
  112. execute()
  113. {
  114.   execvp(name, args);
  115.   std_err("Cannot execute ");
  116.   std_err(name);
  117.   std_err("\n");
  118.   exit(-1);
  119. }
  120.